"use strict";
/* Controllers */
app.controller("healthScoreListController", ['$scope', '$compile', '$location', '$rootScope', '$routeParams', 'healthScoreListService', function ($scope, $compile, $location, $rootScope, $routeParams, healthScoreListService) {

    var vm = $scope.vm;
    vm.p ={}, vm.result = {};

    vm.p.startDt = $routeParams.dt;
    vm.p.endDt = $routeParams.dt;
    vm.p.orgCnName = $routeParams.orgCnName;
    vm.p.ownerId = $routeParams.ownerId;

    if (typeof(vm.p.orgCnName) == 'undefined' || vm.p.orgCnName == 'all' || vm.p.orgCnName == '0') {
        vm.p.orgCnName = ''
    }
    if (typeof(vm.p.ownerId) == 'undefined' || vm.p.ownerId == 'all' || vm.p.ownerId == '0' || vm.p.orgCnName==vm.p.ownerId) {
        vm.p.ownerId = ''
    }

    var datePicker = $('.date');
    datePicker.datetimepicker({
        locale: 'zh-cn',
        format: 'YYYY-MM-DD'
    });
    datePicker.on('dp.hide', function (e) {
        var currentInput = $(e.currentTarget).find('input');
        var eName = currentInput.attr('name');
        var p = $scope.vm.p;
        p[eName] = currentInput.val();
        $scope.$apply()
    });

    if (typeof(vm.p.startDt) == 'undefined' || vm.p.startDt == '' || vm.p.startDt == '0') {
        vm.p.startDt = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    if (typeof(vm.p.endDt) == 'undefined' || vm.p.endDt == '' || vm.p.endDt == '0') {
        vm.p.endDt = new Date(new Date().getTime() - 86400000).format('yyyy-MM-dd');
    }

    var errorFn = function (result) {
        alert(result);
    };

    $scope.queryReport = function () {
        $scope.listTableStorageHealthScore();
    };

    $scope.listTableStorageHealthScore = function () {
        $("#refreshTable").addClass('disabled');
        $("#list_table").bootstrapTable('showLoading')
        var vm = $scope.vm;
        var params = vm.p;
        var procFn = function (response) {
            var data = response.data;
            $('#list_table').bootstrapTable('hideLoading');
            $("#refreshTable").removeClass('disabled');
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        healthScoreListService.listTableStorageHealthScore(params, procFn, errorFn);
    };

    $scope.initializeController = function () {
        var vm = $scope.vm;

        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
            pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'realHealthScore');

        $scope.listTableStorageHealthScore();
    };

    $scope.initializeController();

}]);

/* Services */
app.service('healthScoreListService', ['ajaxService', function (ajaxService) {

    this.listTableStorageHealthScore = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/healthScore/listAllTableScore.do", successFunction, errorFunction);
    };

}]);

function tableStorageHealthScoreConfigOpt(value, row, index) {
    return [
        '<a type="button" class="btn btn-xs btn-primary" ',
            'href="#/star/cost/health_score_detail/', row.dt, '/', row.tableName,'" target="_blank">','计算详情',
        '</a>'
    ].join('');
}

function healthScoreItemsFmt(value, row, index) {
    if(row.healthScore>98) {
        return '-';
    }
    if(row.healthScore==10) {
        return '生命周期未设置'+'<a href="https://dp.58corp.com/data-map/detail-page/'+row.id+'" target="_blank"> 去设置 </a>';
    }else {
        return '生命周期过长'+'<a href="https://dp.58corp.com/data-map/detail-page/'+row.id+'" target="_blank"> 去设置 </a>';
    }
}



function isPartitionFmt(value, row, index) {
    if (value == 0) {
        return "否"
    } else if (value == 1) {
        return "是"
    } else {
        return "-"
    }
}

function filesizeFmt(value, row, index) {
    return internationalNumberShortFormatterStorage(value);
}

/**
 * 数据的国际化格式化方式，三个数据用逗号分隔 如123456转换成123,456
 */
function internationalNumberFormatter(value) {
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
}

function internationalNumberShortFormatterStorage(value) {
    if (value == null || typeof value == 'undefined') {
        return "";
    }
    value += '';
    var x = value.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    var times = 0;
    while (rgx.test(x1)) {
        times++;
        //		x1 = x1.replace(rgx, '$1');
        x1 = changeTwoDecimalFx(x1 / 1024);
    }

    switch (times) {
        case 0:
            return internationalNumberFormatter(value) + "B";
        case 1:
            return x1 + "KB";
        case 2:
            return x1 + "MB";
        case 3:
            return x1 + "GB";
        case 4:
            return x1 + "TB";
        case 5:
            return x1 + "PB";
        case 6:
            return x1 + "EB";
        case 7:
            return x1 + "ZB";
        case 8:
            return x1 + "YB";
        default:
            return internationalNumberFormatter(value);
    }
}

function changeTwoDecimalFx(x) {
    var fx = parseFloat(x);
    if (isNaN(fx)) {
        alert('function:changeTwoDecimal->parameter error');
        return false;
    }
    fx = Math.round(fx * 100) / 100;
    var s_x = fx.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}